package com.aizhixin.lab.task.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.service.UserService;
import com.aizhixin.lab.task.core.TaskInforCode;
import com.aizhixin.lab.task.domain.StuAnswerParamsDomain;
import com.aizhixin.lab.task.domain.StuAnswerSaveDomain;
import com.aizhixin.lab.task.entity.ReleaseTask;
import com.aizhixin.lab.task.entity.ReleaseTaskQuestion;
import com.aizhixin.lab.task.entity.TaskStuInfor;

import java.util.*;

public class PaperRunable implements Runnable {
    private StuAnswerSaveDomain stuAnswerSaveDomain;
    private List<String> stuAnswerIds;
    private Logger logger= LoggerFactory.getLogger(PaperRunable.class);
    private TaskStuInforService taskStuInforService;
    private ReleaseTaskQuestionsService releaseTaskQuestionsService;
    private ReleaseTaskService releaseTaskService;
    private UserService userService;
    public PaperRunable( StuAnswerSaveDomain stuAnswerSaveDomain, List<String> stuAnswerIds,TaskStuInforService taskStuInforService,ReleaseTaskQuestionsService releaseTaskQuestionsService,ReleaseTaskService releaseTaskService,UserService userService) {
        this.stuAnswerSaveDomain = stuAnswerSaveDomain;
        this.stuAnswerIds = stuAnswerIds;
        this.taskStuInforService=taskStuInforService;
        this.releaseTaskQuestionsService=releaseTaskQuestionsService;
        this.releaseTaskService=releaseTaskService;
        this.userService = userService;
    }

    @Override
    public void run() {
        logger.info("自动判分开始---");
        String taskStuId=stuAnswerSaveDomain.getTaskStuId();
        ReleaseTask workRelease= releaseTaskService.findById(stuAnswerSaveDomain.getReleaseTaskId());
        TaskStuInfor  workStuInfo = taskStuInforService.findById(taskStuId);
        String taskStatus = workStuInfo.getTaskStatus();
        workStuInfo.setTaskStatus(TaskInforCode.TASK_STATUS_PENDING);
        workStuInfo.setCommitTime(new Date());
        workStuInfo=taskStuInforService.save(workStuInfo);
        if(TaskInforCode.TASK_STATUS_UNCOMMIT.equals(taskStatus)){
            //更新提交次数
            Integer commitNumber = workRelease.getCommitNum();
            if (commitNumber == null || commitNumber <= 0) {
                commitNumber = 1;
            } else {
                commitNumber = commitNumber + 1;
            }
            workRelease.setCommitNum(commitNumber);
//            //修改待批阅次数
//            Integer pendingNumber = workRelease.getPendingNumber();
//            if (pendingNumber == null || pendingNumber <= 0) {
//                pendingNumber = 1;
//            } else {
//                pendingNumber = pendingNumber + 1;
//            }
//            workRelease.setPendingNumber(pendingNumber);
            releaseTaskService.save(workRelease);
        }
        boolean isPass=true;
        List<ReleaseTaskQuestion> workQuestionsbDomains=releaseTaskQuestionsService.findByReleaseTaskIdOrderByOrderNumAsc(workRelease.getId());
        if (null!=workQuestionsbDomains&&0<workQuestionsbDomains.size()){
            for (ReleaseTaskQuestion workStuQusetionsDomain:workQuestionsbDomains){
                if (workStuQusetionsDomain.getQuestionType()>=40){
                    isPass=false;
                    break;
                }
            }
        }
        Map<String,String> mapData=new HashMap<>();
        if (null!=workRelease&&workRelease.getScoringMethod()==0&&isPass){
            List<StuAnswerParamsDomain> stuAnswerParamsDomainList=stuAnswerSaveDomain.getAnswerList();
            if(null!=stuAnswerParamsDomainList&&0<stuAnswerParamsDomainList.size()){
               for (StuAnswerParamsDomain stuAnswerParamsDomain:stuAnswerParamsDomainList){
                   mapData.put(stuAnswerParamsDomain.getReleaseQuestionId(),stuAnswerParamsDomain.getStuAnswer());
               }
            }
            Integer correct=0;
            for (ReleaseTaskQuestion workStuQusetionsDomain:workQuestionsbDomains){
                String stuAnswer=mapData.get(workStuQusetionsDomain.getId());
                if(StringUtils.isEmpty(stuAnswer)){
                    continue;
                }
                if ((workStuQusetionsDomain.getQuestionType()==10||workStuQusetionsDomain.getQuestionType()==30)&&stuAnswer.equals(workStuQusetionsDomain.getAnswer())){
                    correct++;
                }
                if (workStuQusetionsDomain.getQuestionType()==20){
                    List<String> answerList=new ArrayList<>();
                    char[] chars=workStuQusetionsDomain.getAnswer().toCharArray();
                    for (int i=0;i<chars.length;i++){
                        answerList.add(chars[i]+"");
                    }
                    if (!StringUtils.isEmpty(stuAnswer)){
                        char[] stuAnwsers=stuAnswer.toCharArray();
                        if (answerList.size()==stuAnwsers.length){
                            boolean isCorrect=true;
                            for (int i=0;i<stuAnwsers.length;i++){
                                if (!answerList.contains(stuAnwsers[i]+"")){
                                    isCorrect=false;
                                    break;
                                }
                            }
                            if (isCorrect){
                                correct++;
                            }
                        }
                    }
                }
            }
            String score="";
            if (correct!=0){
                Double s=Double.parseDouble(correct+"")/Double.parseDouble(workQuestionsbDomains.size()+"")*100;
                score= Math.round(s)+"";
            }else{
                score=correct+"";
            }
            User accountDTO =null;
            try {
                 accountDTO = userService.getUser(null, workRelease.getCreatedBy());
            }catch (Exception e){

            }
            if (accountDTO!=null) {
            	taskStuInforService.updateworkStuInfo(taskStuId, score, null, stuAnswerIds, true, accountDTO.getName());
                logger.info("自动判分结束---");
            }else {
            	taskStuInforService.updateworkStuInfo(taskStuId, score, null, stuAnswerIds, true, "");
                logger.info("自动判分结束---");
            }
        }
    }
}
